package com.wj.rpsm.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wj.rpsm.admin.mapper.UserMapper;
import com.wj.rpsm.admin.pojo.PageResult;
import com.wj.rpsm.admin.pojo.user.LoginUser;
import com.wj.rpsm.admin.pojo.user.ResUser;
import com.wj.rpsm.admin.pojo.user.User;
import com.wj.rpsm.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author WeiJin
 * @version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public ResUser Login(LoginUser user) {

        List<User> users = userMapper.queryOnCondition(User.toUser(user));
        return users.size() == 1 ? users.get(0).toResUser() : null;
    }

    @Override
    public PageResult<User> pageOfUser(Integer current, Integer size, String condition, Integer user_type) {
        PageHelper.startPage(current, size);
        Page<User> users = userMapper.findUserByType(condition, user_type);
        return PageResult.restPage(users);
    }

    @Override
    public User searchById(int id){
        return userMapper.selectById(id);
    }

    @Override
    public Integer updateAvatar(User user) {
        return userMapper.updateAvatar(user);
    }
}
